package cn.com.hhrcw.enterprise.web;

import cn.com.hhrcw.enterprise.params.OpFilterParams;
import cn.com.hhrcw.enterprise.vo.OpFilterVO;
import cn.com.hhrcw.entity.OpFilter;
import cn.com.hhrcw.mapstruct.EnOpFilterConvert;
import cn.com.hhrcw.service.IOpFilterService;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.AttributesValueParams;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.function.Function;

/**
 * 职位-筛选条件
 *
 * @author jiangdong
 * @since 2020-10-29
 */
@RestController
@Slf4j
@RequestMapping("/en/opFilter")
@Api(tags = "职位-筛选条件")
@Validated
public class EnOpFilterController extends JeecgController<OpFilter, IOpFilterService> {

    @Autowired
    private IOpFilterService opFilterService;

    @Autowired
    private EnOpFilterConvert opFilterConvert;

    /**
     * 分页列表查询
     *
     * @param params 请求参数
     * @return
     */
    @GetMapping(value = "/list")
    @AutoLog(logType = 2, ty = "visit", value = "职位筛选条件列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PermissionData
    public Result<Page<OpFilterVO>> list(OpFilterParams params, HttpServletRequest request) {
        QueryWrapper<OpFilter> queryWrapper = QueryGenerator.initQueryWrapper(opFilterConvert.toEntity(params), request.getParameterMap());
        queryWrapper.eq("del_flag", false);
        if (StringUtils.isBlank(params.getField())) {
            queryWrapper.orderByDesc("create_time");
        } else {
            queryWrapper.orderBy(true, params.getSort(), toUnderlineName(params.getField()));
        }
        IPage<OpFilter> page = opFilterService.page(params.page(), queryWrapper);
        //分页结果转换器
        Function<OpFilter, OpFilterVO> fun = opFilter -> opFilterConvert.toVO(opFilter);

        return Result.ok((Page) page.convert(fun));
    }

    /**
     * 根据id查询
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/queryById")
    @AutoLog(logType = 2, ty = "visit", value = "职位筛选条件查询")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @PermissionData
    public Result<OpFilterVO> queryById(String id) {
        OpFilter opFilter = opFilterService.lambdaQuery().eq(OpFilter::getId, id).eq(OpFilter::getDelFlag, false).one();
        if (opFilter == null) {
            throw new JeecgBootException("数据不存在");
        }
        return Result.ok(opFilterConvert.toVO(opFilter));
    }

    /**
     * 添加
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/add")
    @AutoLog(logType = 2, ty = "insert", value = "职位筛选条件添加")
    @ApiOperation(value = "添加", notes = "添加")
    @RequiresPermissions("opFilter:add")
    public Result<?> add(@RequestBody @Valid OpFilterParams params) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        OpFilter opFilter = opFilterConvert.toEntity(params);
        opFilter.setCreateBy(sysUser.getId());
        opFilter.setCreateTime(DateUtil.date());
        opFilter.setDelFlag(false);
        boolean saved = opFilterService.save(opFilter);
        return saved ? Result.ok(opFilter.getId()) : Result.error("添加失败");
    }

    /**
     * 编辑
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/edit")
    @AutoLog(logType = 2, ty = "update", value = "职位筛选条件编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @RequiresPermissions("opFilter:edit")
    public Result<?> edit(@RequestBody @Valid OpFilterParams params) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        OpFilter opFilter = opFilterConvert.toEntity(params);
        opFilter.setUpdateBy(sysUser.getId());
        opFilter.setUpdateTime(DateUtil.date());

        boolean updated = opFilterService.lambdaUpdate()
                .set(OpFilter::getUpdateBy, opFilter.getUpdateBy()).set(OpFilter::getUpdateTime, opFilter.getUpdateTime())
                .set(OpFilter::getLanguage, opFilter.getLanguage()).set(OpFilter::getSex, opFilter.getSex())
                .set(OpFilter::getJobMaxAge, opFilter.getJobMaxAge()).set(OpFilter::getJobMinAge, opFilter.getJobMinAge()).set(OpFilter::getMaxExperience, opFilter.getMaxExperience())
                .set(OpFilter::getMinExperience, opFilter.getMinExperience()).set(OpFilter::getJobRequire, opFilter.getJobRequire())
                .set(OpFilter::getEduLevel, opFilter.getEduLevel()).set(OpFilter::getJobSubject, opFilter.getJobSubject()).eq(OpFilter::getId, opFilter.getId()).update();
        return updated ? Result.ok() : Result.error("编辑失败");
    }

    /**
     * 批量删除
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/delete")
    @AutoLog(logType = 2, ty = "delete", value = "职位筛选条件删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @RequiresPermissions("opFilter:delete")
    public Result delete(String[] id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaUpdateWrapper<OpFilter> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(OpFilter::getId, id).set(OpFilter::getDelFlag, true).set(OpFilter::getUpdateBy, sysUser.getUsername()).set(OpFilter::getUpdateTime, DateUtil.date());

        boolean deleted = opFilterService.update(updateWrapper);
        return deleted ? Result.ok() : Result.error("删除失败");
    }

    /**
     * 属性值是否重复
     *
     * @return
     */
    @Override
    @GetMapping("/propertyRepeat")
    @ApiOperation(value = "属性值是否重复", notes = "属性值是否重复")
    public Result propertyIsRepeat(@Valid AttributesValueParams params) {
        return super.propertyIsRepeat(params);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param params
     */
    @SneakyThrows
    @GetMapping(value = "/exportXls")
    @ApiOperation(value = "导出excel", notes = "导出excel")
    public Result exportXls(HttpServletRequest request, OpFilterParams params) {
        OpFilter opFilter = opFilterConvert.toEntity(params);
        opFilter.setDelFlag(false);
        QueryWrapper<OpFilter> queryWrapper = QueryGenerator.initQueryWrapper(opFilter, request.getParameterMap());
        List<OpFilter> data = opFilterService.list(queryWrapper);
        ExportParams exportParams = new ExportParams("职位-筛选条件", "职位-筛选条件");
        String url = super.exportXls(exportParams, OpFilterVO.class, opFilterConvert.toVO(data));
        return Result.ok((Object) url);
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OpFilter.class);
    }

}
